<?php
/**
 * api controller
 * @package app-member_controller
 */
load_controller('/memberbase');
/**
 * api controller
 * @author pandajingjing
 * @package app-member_controller
 */
class apicontroller extends memberbasecontroller{

	/**
	 * 在控制器开始时执行（调度使用）
	 * @see app_controller::beforeRequest()
	 */
	function beforeRequest(){
		parent::beforeRequest();
		$strAppKey=$this->getParam('k','url');
		$strAppSig=$this->getParam('s','url');
		$intCallTime=$this->getParam('t','url','int');
		$intNowTime=$this->getTime();
		$strReturnURL='';
		load_lib('/bll/member/app');
		$clsApp=new bll_member_app();
		$mixAppChk=$clsApp->chkAppValid($strAppKey,$strAppSig,$intCallTime,$intNowTime);
		if(true===$mixAppChk){
			$strActSig=$this->getParam('as','url');
			$strBase64ReturnURL=$this->getParam('u','url');
			$strTmpReturnURL=base64_decode($strBase64ReturnURL);
			$mixActChk=$clsApp->chkActionValid($strAppKey,$strActSig,$strTmpReturnURL,$intCallTime);
			if(true===$mixActChk){
				$strReturnURL=$strTmpReturnURL;
			}else{
				$mixAppChk=$mixActChk;
			}
		}elseif('Sys_Error_NoApp'==$mixAppChk){
			$this->addHeader('HTTP/1.0 404 Not Found');
		}
		$this->setData('mixAppChk',$mixAppChk);
		$this->setData('strReturnURL',$strReturnURL);
	}

	/**
	 * 入口函数（调度使用）
	 * @see sys_controller::doRequest()
	 */
	function doRequest(){
		$strAction=$this->getParam('action','url');
		$arrErrInfo=array();
		$mixAppChk=$this->getData('mixAppChk');
		if(true===$mixAppChk){
			$strReturnURL=$this->getData('strReturnURL');
			switch($strAction){
				case 'login':
					$strAction=$this->getParam('hidAction','post');
					if('login'==$strAction){
						$strLoginValue=$this->getParam('txtLoginValue','post');
						$strUserPwd=$this->getParam('pwdUserPwd','post');
						load_lib('/bll/member/login');
						$clsLogin=new bll_member_login();
						$arrUserInfo=array();
						$mixResult=$clsLogin->loginUser($strLoginValue,$strUserPwd,$this->getClientIP(),$this->getTime(),$arrUserInfo);
						if(is_array($mixResult)){
							$strReturnURL=self::mergeURL($strReturnURL,array( 
									'status'=>'fail',
									'txtLoginValue'=>base64_encode($strLoginValue),
									'errmsg'=>base64_encode(json_encode($mixResult)) 
							));
							$this->redirectURL($strReturnURL);
						}else{
							$this->setCookie('mauthcode',$clsLogin->encryptLoginInfo(array( 
									'USERID'=>$arrUserInfo['USERID'],
									'USERNAME'=>$arrUserInfo['USERNAME'] 
							)),31536000);
							$strReturnURL=self::mergeURL($strReturnURL,array( 
									'status'=>'ok' 
							));
							load_lib('/bll/member/app');
							$clsApp=new bll_member_app();
							$arrAutoLoginList=$clsApp->getAutoLoginURL($arrUserInfo['USERID'],$arrUserInfo['USERNAME'],$this->getTime(),array( 
									'LASTUPD'=>$arrUserInfo['LASTUPD'] 
							));
							$this->setData('strReturnURL',$strReturnURL);
							$this->setData('arrAutoLoginList',$arrAutoLoginList);
							return '/loginapp';
						}
					}
					break;
				case 'reg':
					$strAction=$this->getParam('hidAction','post');
					if('reg'==$strAction){
						$strUserName=$this->getParam('txtUserName','post');
						$strUserPwd=$this->getParam('pwdUserPwd','post');
						$strUserPwdCfm=$this->getParam('pwdUserPwdCfm','post');
						load_lib('/bll/member/login');
						$clsBll=new bll_member_login();
						$mixResult=$clsBll->regUser($strUserName,$strUserPwd,$strUserPwdCfm,$this->getClientIP(),$this->getTime());
						if(is_array($mixResult)){
							$strReturnURL=self::mergeURL($strReturnURL,array( 
									'status'=>'fail',
									'txtUserName'=>base64_encode($strUserName),
									'errmsg'=>base64_encode(json_encode($mixResult)) 
							));
							$this->redirectURL($strReturnURL);
						}else{
							$arrUserInfo=array( 
									'USERID'=>$mixResult,
									'USERNAME'=>$strUserName 
							);
							$this->setCookie('mauthcode',$clsBll->encryptLoginInfo($arrUserInfo),31536000);
							$strReturnURL=self::mergeURL($strReturnURL,array( 
									'status'=>'ok' 
							));
							load_lib('/bll/member/app');
							$clsApp=new bll_member_app();
							$arrAutoLoginList=$clsApp->getAutoLoginURL($arrUserInfo['USERID'],$arrUserInfo['USERNAME'],$this->getTime());
							$this->setData('strReturnURL',$strReturnURL);
							$this->setData('arrAutoLoginList',$arrAutoLoginList);
							return '/loginapp';
						}
					}
					break;
				case 'logout':
					$strAuthCode=$this->getParam('mauthcode','c');
					load_lib('/bll/member/base');
					$clsBll=new bll_member_base();
					$arrUserInfo=$clsBll->decryptLoginInfo($strAuthCode);
					$this->setCookie('mauthcode','',-1);
					load_lib('/bll/member/app');
					$clsApp=new bll_member_app();
					$arrAutoLogoutList=$clsApp->getAutoLogoutURL($arrUserInfo['USERID'],$arrUserInfo['USERNAME'],$this->getTime());
					$this->setData('strReturnURL',$strReturnURL);
					$this->setData('arrAutoLogoutList',$arrAutoLogoutList);
					return '/logoutapp';
					break;
				default:
					break;
			}
		}else{
			$arrErrInfo[]=$mixAppChk;
		}
		//$this->addHeader('Content-type:text/json');
		$this->setData('arrErrInfo',$arrErrInfo);
		$this->setData('jsonErrInfo',json_encode($arrErrInfo));
		return '/api';
	}

}